import config from '@/common/config.js'
const baseUrl = config.host


const request = function(url, data , cb){
	let headerObj = {
		'Content-Type': 'application/json'
	}
	uni.showLoading({
		title: '加载中'
	});
	uni.request({
		header: headerObj,
		url: baseUrl + url,
		method: 'POST',
		data: data,
		success: function(res) {
			// console.log("接口返回",res);
			uni.hideLoading();
			if (res.data.status == 200) {
				return typeof cb == "function" && cb(res.data);
			}else if(res.data.status == 500) {
				toast(res.data.msg || url + "接口异常");
			} else {
				toast(res.data.msg || "系统错误");
			}
		},
		complete(e) {
			uni.hideLoading();
		}
	})
}

/* 提示弹框 */
const toast = function(msg) {
	uni.showToast({
		title: msg,
		icon: 'none',
		position: 'bottom',
		duration: 2000
	})
}
/* 提示弹框 */
const confirm = function(msg, cb) {
	uni.showModal({
		title: '提示',
		content: msg,
		success: function(res) {
			if (res.confirm) {
				return typeof cb == "function" && cb()
			}
		}
	});
}

export default {
	request,
	toast,
	confirm
};
